﻿#include "result.h"
#include "ui_result.h"
#include <iostream>
#include <vector>
#include <numeric>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/xfeatures2d.hpp>
#include "robustMatcher.h"
#include "mainwindow.h"
#include <QtDebug>
#pragma execution_character_set("utf-8")
result::result(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::result)
{
    ui->setupUi(this);
    setWindowTitle("结果显示页面");
    setGeometry(150,200,950,730);

}

result::~result()
{
    delete ui;
}

void result::check()
{
    // Read input images
    using namespace cv;
    if(qold1!="" && qold2!=""){
        QByteArray arr1 = qold1.toUtf8();
        oldimgpath1=arr1.data();
        QByteArray arr2 = qold2.toUtf8();
        oldimgpath2=arr2.data();

        QPixmap *oldImg1=new QPixmap(qold1);
        ui->oldimg1->setScaledContents(true);
        oldImg1->scaled(ui->oldimg1->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        ui->oldimg1->setPixmap(*oldImg1);

        QPixmap *oldImg2=new QPixmap(qold2);
        ui->oldimg2->setScaledContents(true);
        oldImg2->scaled(ui->oldimg2->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        ui->oldimg2->setPixmap(*oldImg2);


        cv::Mat image1= cv::imread(oldimgpath1,0);
        cv::Mat image2= cv::imread(oldimgpath2,0);

        RobustMatcher rmatcher(cv::ORB::create(300,
                                               2.2,
                                               8));


        std::vector<cv::DMatch> matches;
        std::vector<cv::KeyPoint> keypoints1, keypoints2;
        cv::Mat fundamental = rmatcher.match(image1, image2, matches,
            keypoints1, keypoints2);


        cv::Mat imageMatches;
        cv::drawMatches(image1, keypoints1,
            image2, keypoints2,
            matches,
            imageMatches,
            cv::Scalar(255, 255, 255),
            cv::Scalar(255, 255, 255),
            std::vector<char>(),
            2);

        ui->matchimg->setScaledContents(true);
        QImage imgmatch = QImage(imageMatches.data, imageMatches.cols, imageMatches.rows, imageMatches.step, QImage::Format_RGB888);
        ui->matchimg->setPixmap(QPixmap::fromImage(imgmatch.rgbSwapped()).scaled(ui->matchimg->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));


        std::vector<cv::Point2f> points1, points2;

        for (std::vector<cv::DMatch>::const_iterator it = matches.begin();
        it != matches.end(); ++it) {

            float x = keypoints1[it->queryIdx].pt.x;
            float y = keypoints1[it->queryIdx].pt.y;
            points1.push_back(keypoints1[it->queryIdx].pt);

            x = keypoints2[it->trainIdx].pt.x;
            y = keypoints2[it->trainIdx].pt.y;
            points2.push_back(keypoints2[it->trainIdx].pt);
        }

        cv::Mat h1, h2;
        cv::stereoRectifyUncalibrated(points1, points2, fundamental, image1.size(), h1, h2);

        cv::Mat rectified1;
        cv::warpPerspective(image1, rectified1, h1, image1.size());
        cv::Mat rectified2;
        cv::warpPerspective(image2, rectified2, h2, image1.size());

        points1.clear();
        points2.clear();
        for (int i = 20; i < image1.rows - 20; i += 20) {
            points1.push_back(cv::Point(image1.cols / 2, i));
            points2.push_back(cv::Point(image2.cols / 2, i));
        }

        std::vector<cv::Vec3f> lines1;
        cv::computeCorrespondEpilines(points1, 1, fundamental, lines1);

        for (std::vector<cv::Vec3f>::const_iterator it = lines1.begin();
        it != lines1.end(); ++it) {
            cv::line(image2, cv::Point(0, -(*it)[2] / (*it)[1]),
                cv::Point(image2.cols, -((*it)[2] + (*it)[0] * image2.cols) / (*it)[1]),
                cv::Scalar(255, 255, 255));
        }

        std::vector<cv::Vec3f> lines2;
        cv::computeCorrespondEpilines(points2, 2, fundamental, lines2);

        for (std::vector<cv::Vec3f>::const_iterator it = lines2.begin();
        it != lines2.end(); ++it) {
            cv::line(image1, cv::Point(0, -(*it)[2] / (*it)[1]),
                cv::Point(image1.cols, -((*it)[2] + (*it)[0] * image1.cols) / (*it)[1]),
                cv::Scalar(255, 255, 255));
        }

        cv::drawMatches(image1, keypoints1,
            image2, keypoints2,
            std::vector<cv::DMatch>(),
            imageMatches,
            cv::Scalar(255, 255, 255),
            cv::Scalar(255, 255, 255),
            std::vector<char>(),
            2);

        ui->bothline->setScaledContents(true);
        QImage bline = QImage(imageMatches.data, imageMatches.cols, imageMatches.rows, imageMatches.step, QImage::Format_RGB888);
        ui->bothline->setPixmap(QPixmap::fromImage(bline.rgbSwapped()).scaled(ui->bothline->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));

        cv::Mat disparity;
        cv::Ptr<cv::StereoMatcher> pStereo = cv::StereoSGBM::create(0,
                                                                    32,
                                                                    5);
        pStereo->compute(rectified1, rectified2, disparity);

        double minv, maxv;
        disparity = disparity * 64;
        cv::minMaxLoc(disparity, &minv, &maxv);
        info=info+rmatcher.info;
        info=info+QString::number(minv)+"+"+QString::number(maxv)+"\n";
        ui->resultout->setText(info);
        cv::namedWindow("result");
        cv::imshow("result", disparity);
    }else{
        ui->resultout->setText("原图片不存在");
    }
}
